package service

import (
	"apitestAfterGo/dao"
	"apitestAfterGo/domain"
	"apitestAfterGo/dto"
	"github.com/zhuxiujia/GoMybatis"
	"strings"
)

// 事务管理

type urlService struct {
	urlsMapper            *domain.UrlsMapper
	List                  func(urls string) ([]domain.Urls, error)         `tx:"" rollback:"error"`
	FindAllUrls           func() ([]domain.Urls, error)                    `tx:"" rollback:"error"`
	RemoveUrl             func(id int) (int64, error)                      `tx:"" rollback:"error"`
	AddUrl                func(urls domain.Urls) (int64, error)            `tx:"" rollback:"error"`
	SelectTemplate        func(id int) (domain.Urls, error)                `tx:"" rollback:"error"`
	GetAllUrls            func(dto dto.UrlInputDto) ([]domain.Urls, error) `tx:"" rollback:"error"`
	GetUrlsByUser         func(urls string) ([]domain.Urls, error)         `tx:"" rollback:"error"`
	UpdateUrlById         func(url domain.Urls) (int64, error)             `tx:"" rollback:"error"`
	GetAllUrlsCountByPage func(dto dto.UrlInputDto) (int, error)           `tx:"" rollback:"error"`
	UpdateUrlByUserId     func(dto dto.UrlEditInputDto) (int, error)       `tx:"" rollback:"error"`
	GetUrlsByUserId       func(id int) (string, error)                     `tx:"" rollback:"error"`
}

var ListServiceImpl urlService

func init() {
	ListServiceImpl = urlService{
		urlsMapper: &dao.UrlsMapper,
		List: func(urls string) ([]domain.Urls, error) {
			split := strings.Split(urls, ",")
			us, _ := ListServiceImpl.urlsMapper.FindUrlsById(split)
			for i := range us {
				dto := dto.UrlSqlDto{
					Id:   us[i].Id,
					Urls: split,
				}
				us[i].Children, _ = ListServiceImpl.urlsMapper.FindUrlsByCurrentId(dto)
			}
			return us, nil
		},
		FindAllUrls: func() ([]domain.Urls, error) {
			urls, _ := ListServiceImpl.urlsMapper.FindAllUrls()
			for i := range urls {
				urls[i].Children, _ = ListServiceImpl.urlsMapper.FindUrlsByParentId(urls[i].Id)
			}
			return urls, nil
		},
		RemoveUrl: func(id int) (int64, error) {
			return ListServiceImpl.urlsMapper.DeleteTemplate(id)
		},
		AddUrl: func(urls domain.Urls) (int64, error) {
			return ListServiceImpl.urlsMapper.AddUrl(urls)
		},
		SelectTemplate: func(id int) (domain.Urls, error) {
			return ListServiceImpl.urlsMapper.SelectTemplate(id)
		},
		GetAllUrls: func(dto dto.UrlInputDto) ([]domain.Urls, error) {
			dto.Current = (dto.Current - 1) * dto.PageSize
			urls, err := ListServiceImpl.urlsMapper.GetAllUrls(dto)
			for i := range urls {
				urls[i].Children, _ = ListServiceImpl.urlsMapper.FindUrlsByParentId(urls[i].Id)
			}
			return urls, err
		},
		GetUrlsByUser: func(urls string) ([]domain.Urls, error) {
			return ListServiceImpl.urlsMapper.GetUrlsByUser(strings.Split(urls, ","))
		},
		UpdateUrlById: func(url domain.Urls) (int64, error) {
			return ListServiceImpl.urlsMapper.UpdateUrlById(url)
		},
		GetAllUrlsCountByPage: func(dto dto.UrlInputDto) (int, error) {
			return ListServiceImpl.urlsMapper.GetAllUrlsCountByPage(dto)
		},
		UpdateUrlByUserId: func(dto dto.UrlEditInputDto) (int, error) {
			return ListServiceImpl.urlsMapper.UpdateUrlByUserId(dto)
		},
		GetUrlsByUserId: func(id int) (string, error) {
			return ListServiceImpl.urlsMapper.GetUrlsByUserId(id)
		},
	}
	GoMybatis.AopProxyService(&ListServiceImpl, &dao.Engine)
}
